<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class menu extends My_Controller {

    var $name = 'menu';
    
    public function __construct()
    {
        parent::__construct();
        $this->load->model('menu_model');
    }
    
	public function index()
	{
        $this->list_menu(0);
	}
    
    function list_menu($menuId = 0)
    {
        $menu = $this->menu_model->menuParent($menuId,false);

       $data['menu'] = $menu;
       
       $data_sidebar = array(
            'menu' => 'menu'
       );
       $this->template->parse_view('sidebarleft','default/sidebarleft/sidebarleft',$data_sidebar);
       $this->template->parse_view('content', 'default/content/list-menu-content', $data);
       // Render template
        $this->template->render();
    }
    
    
    function listController()//xong
    {
        $this->load->model('menu_model');
        
        $Toolbar = new MY_ButtonToolbar();
        $button = array(
            array(
                'type' => 'back',
                'link' => '?option=menu&view=menumanager',
                'text' => $this->lang->line('btn_back')
            )
        );
        $menuid = $this->input->get('menu');
        $key_menu = '';
        if($menuid)
        {
            $key_menu = '&menu='.$menuid;
        }
        
        $data['action'] = '';
        $data['loadcontroller'] = $this->menu_model->load_controller($key_menu);
        $data['site_title'] = $this->lang->line('title_2');
        $data['site_toolbar'] = $Toolbar->insertToolbar($button);
        $data['view_item'] = 'menu/controller_view';
        $this->load->view('template',$data);
    }
    
    function menuDetails()//xong
    {
        $this->load->model('menu_model');
        
        $id = $this->input->get('id');
        $c = $this->input->get('c');// neu co hanh dong thay doi duong dan
        $islink = $this->input->get('islink'); //neu la them dia chi tu do
        
        $menu_id = $this->input->post('menu_id');
        
        $act = $this->input->post('act');
        
        $title = $this->input->post('title');
        $published = $this->input->post('published');
        $stt = $this->input->post('stt');
        $link = $this->input->post('link');
        $parent = $this->input->post('parent');
        
        $row_menu = $this->menu_model->get_menu_item($id);
        
        $Toolbar = new MY_ButtonToolbar();
        $button = array(
            array(
                'type' => 'save',
                'link' => '',
                'text' => $this->lang->line('btn_save')
            ),
            array(
                'type' => 'delete',
                'link' => '?option=menu&view=deletemenu&act=do&id='.$row_menu->id,
                'text' => $this->lang->line('btn_delete')
            ),
            array(
                'type' => 'cancel',
                'link' => '?option=menu&view=menumanager',
                'text' => $this->lang->line('btn_cancel')
            )
            
        );
        $menulink = '';
        ($c == 1)?$menulink=$this->session->userdata('session_menulink'):$menulink=$row_menu->link;
        $flag = true;
        switch($act){
            case 'do':
                if($title ==''){
                    $flag = false;
                    $this->style_model->error[] = $this->lang->line('mn_error_0');
                }
                if($flag && $menu_id == $id)
                {
                    $parent_ins = 0;
                    $dirparent_ins = 0;
                    
                    if($parent == 0)
                    {
                        $parent_ins = 0;
                        $dirparent_ins = 0;
                        //$this->site_model->update_menu_parent($menu->id,$menu->id);
                        $parent_item = $row_menu->id;
                        $dirparent_item = $row_menu->id;
                    }
                    else
                    {
                        $menu_chose = $this->menu_model->get_menu_item($parent);
                        if($menu_chose->parent == 0 && $menu_chose->dirparent == 0)
                        {
                            $parent_ins = $menu_chose->id;
                            $dirparent_ins = $menu_chose->id;
                            // update lai toan bo menu con
                            //$this->site_model->update_menu_parent($menu->id,$dirparent_ins);
                            $parent_item = $row_menu->id;
                            $dirparent_item = $dirparent_ins;
                        }
                        else
                        {
                            $parent_ins = $menu_chose->id;
                            $dirparent_ins = $menu_chose->dirparent;
                            // update lai toan bo menu con
                            //$this->site_model->update_menu_parent($menu->id,$dirparent_ins);
                            $parent_item = $row_menu->id;
                            $dirparent_item = $dirparent_ins;
                        }
                    }
                    if($islink == 1)
                        $updatedata = array(
                            'parent' => $parent_ins,
                            'dirparent' => $dirparent_ins,
                            'title' => $title,
                            'link' => $link,
                            'is_link' => 1,
                            'stt' => $stt,
                            'published' => $published
                        );
                    else
                        $updatedata = array(
                            'parent' => $parent_ins,
                            'dirparent' => $dirparent_ins,
                            'title' => $title,
                            'link' => $link,
                            'is_link' => 0,
                            'stt' => $stt,
                            'published' => $published
                        );
                    $this->menu_model->updateMenu($row_menu->id,$updatedata);
                    $this->menu_model->update_menu_parent($parent_item,$dirparent_item);

                    redirect('option=menu&view=menumanager', 'location', 301);
                }else{
                    $menu = array(
                        'title' => $title,
                        'published' => $published,
                        'stt' => $stt,
                        'link' => $link,
                        'id' => $id
                    );
                }
            break;
        
            default:
                $menu = array(
                    'title' => $row_menu->title,
                    'published' => $row_menu->published,
                    'stt' => $row_menu->stt,
                    'link' => $menulink,
                    'id' => $row_menu->id
                );
            break;
        }

        $data['menu'] = $menu;
        $data['action'] = '?option=menu&view=menuDetails&id='.$row_menu->id.'&islink='.$islink;
        $data['site_title'] = $this->lang->line('title_3');
        $data['site_toolbar'] = $Toolbar->insertToolbar($button);
        
        //$data['title'] = $menu->title;
        //$data['published'] = $menu->published;
        
        
        
        //$data['link'] = $menu->link;
        //$data['stt'] = $menu->stt;
        //$data['menu_id'] = $menu->id;
        
        $data['loadoptionmenu'] = $this->menu_model->show_option_menu(0,'- ','',$id,$row_menu->parent);
        $data['controller'] = $this->name;
        $data['view_item'] = 'menu/menuDetails_view';
        $this->load->view('template',$data);
    }
    
    function newMenu() //xong
    {
        $this->load->model('menu_model');
        
        $Toolbar = new MY_ButtonToolbar();
        $button = array(
            array(
                'type' => 'save',
                'link' => '',
                'text' => $this->lang->line('btn_save')
            ),
            array(
                'type' => 'cancel',
                'link' => '?option=menu&view=menumanager',
                'text' => $this->lang->line('btn_cancel')
            )
            
        );
        
        $islink = $this->input->get('islink'); //neu la them dia chi tu do
        $menu_id = $this->input->post('menu_id');
        
        $act = $this->input->post('act');
        
        $title = $this->input->post('title');
        $published = $this->input->post('published');
        $stt = $this->input->post('stt');
        $link = $this->input->post('link');
        $parent = $this->input->post('parent');
        $flag = true;
        switch($act)
        {
            case 'do':
            
                if($title ==''){
                    $flag = false;
                    $this->style_model->error[] = $this->lang->line('mn_error_0');
                }
                if($flag)
                {
                    $parent_ins = 0;
                    $dirparent_ins = 0;
                    
                    if($parent == 0)
                    {
                        $parent_ins = 0;
                        $dirparent_ins = 0;
                    }
                    else
                    {
                        $menu_chose = $this->menu_model->get_menu_item($parent);
                        if($menu_chose->parent == 0 && $menu_chose->dirparent == 0)
                        {
                            $parent_ins = $menu_chose->id;
                            $dirparent_ins = $menu_chose->id;
                        }
                        else
                        {
                            $parent_ins = $menu_chose->id;
                            $dirparent_ins = $menu_chose->dirparent;
                        }
                    }
                    if($islink == 1)
                        $insertdata = array(
                            'parent' => $parent_ins,
                            'dirparent' => $dirparent_ins,
                            'title' => $title,
                            'link' => $link,
                            'is_link' => 1,
                            'stt' => $stt,
                            'published' => $published
                        );
                    else
                        $insertdata = array(
                            'parent' => $parent_ins,
                            'dirparent' => $dirparent_ins,
                            'title' => $title,
                            'link' => $link,
                            'is_link' => 0,
                            'stt' => $stt,
                            'published' => $published
                        );
                    $this->menu_model->insertMenu($insertdata);
                    redirect('option=menu&view=menumanager', 'location', 301);
                }
                else
                {
                    $menu = array(
                        'title' => $title,
                        'published' => $published,
                        'stt' => $stt,
                        'link' => $link,
                        'id' => 0
                    );
                    //$data['title'] = $title;
                    //$data['published'] = $published;
                    //$data['link'] = $link;
                    //$data['stt'] = $stt;
                }
                break;
                default:
                    $menu = array(
                        'title' => '',
                        'published' => true,
                        'stt' => 0,
                        'link' => $this->style_model->links,
                        'id' => 0
                    );
                    //$data['title'] = '';
                    //$data['published'] = true;
                    //$data['link'] = $this->style_model->links;
                    //$data['stt'] = 0;
                break;
        }
        $data['menu'] = $menu;
        $data['action'] = '?option=menu&view=newMenu&islink='.$islink;
        $data['site_title'] = $this->lang->line('title_4');
        $data['site_toolbar'] = $Toolbar->insertToolbar($button);
        $data['loadoptionmenu'] = $this->menu_model->show_option_menu(0,'- ','',0);
        //$data['controller'] = $this->name;
        //$data['view_item'] = 'menu/newmenu_view';
        $data['view_item'] = 'menu/menuDetails_view';
        $this->load->view('template',$data);
    }
    
    function deletemenu()
    {
        $this->load->model('menu_model');
        $menu_id = $this->input->get('id');
        $act = $this->input->get('act');
        
        if($act == 'do')
        {
            $this->menu_model->deleteMenu($menu_id);
            $this->menu_model->update_menu_parent_delete($menu_id);
            redirect('option=menu&view=menumanager', 'location', 301);
        }
        else
        {
            redirect('option=menu&view=menumanager', 'location', 301);
        }
    }

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */